<?php

/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: pay_wxqrpay.php 33234 2016-06-03 04:38:05Z mpage $
 */

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

$pay_id = C::t('#dzapp_base#dzapp_base_pay')->insert(array(
	'uid' => $_G['uid'],
	'username' => $_G['username'],
	'subject' => $param['subject'],
	'id' => $param['id'],
	'table' => $param['table'],
	'return' => $param['return'],
	'plugin' => $param['plugin'],
	'price' => $param['price'],
	'money' => $param['money'],
	'pay_type' => 'wxpay',
	'add_time' => $_G['timestamp'],
	'ip' => $_G['clientip'],
), true);

if($_G['mobile']) {

	include_once DISCUZ_ROOT.'/source/plugin/dzapp_wxpay/class/payPubHelper.class.php';
	$jsApi = new JsApi_pub();
	$unifiedOrder = new UnifiedOrder_pub();
	$unifiedOrder->setParameter("openid", $_G['cookie']['openid']);
	$unifiedOrder->setParameter("body", diconv($param['subject'], CHARSET, 'UTF8'));
	$unifiedOrder->setParameter("out_trade_no", $pay_id); 
	$unifiedOrder->setParameter("total_fee", $param['price'] * 100);
	$unifiedOrder->setParameter("notify_url", $_G['siteurl'].'source/plugin/dzapp_wxpay/api/notify.php');
	$unifiedOrder->setParameter("trade_type", "JSAPI");
	$prepay_id = $unifiedOrder->getPrepayId();
	$jsApi->setPrepayId($prepay_id);
	$jsApiParameters = json_decode($jsApi->getParameters(), true);
	 
	$jsApiParameters['href'] = $param['return'];
	echo json_encode($jsApiParameters);
	exit();

} else {
	showmessage('operation_done', dreferer(), array('pay_type' => 'wxpay', 'pay_id' => $pay_id), array('closetime' => true, 'showdialog' => 0, 'locationtime' => 1));
}

?>